package com.cskaoyan.controller.wxcart;

import com.cskaoyan.bean.MallCart;
import com.cskaoyan.model.bo.wxcart.*;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.wxcart.WxCartTotalListVo;
import com.cskaoyan.model.vo.wxcart.WxCheckoutTotalVo;
import com.cskaoyan.service.wxcart.WxCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @program: project-market
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-02 15:34
 **/
@RestController
@RequestMapping("wx/cart")
public class WxCartController {


    @Autowired
    WxCartService cartService;

    @RequestMapping("index")
    public BaseRespVo index(HttpSession session) {
        // 查询当前用户所有订单信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        WxCartTotalListVo vo = cartService.list(uid);

        return BaseRespVo.ok(vo);

    }


    @RequestMapping("goodscount")
    public BaseRespVo goodscount(HttpSession session) {
        // 查询购物车有多少个商品
        Integer uid = (Integer) session.getAttribute("uid");

        // 如果用户没登录，购物车数据返回0
        if (uid == null) return BaseRespVo.ok(0);

        // 用户登录了，查一下购物车有多少数据
        WxCartTotalListVo totalListVo = cartService.list(uid);
        int cartGoodsCount = totalListVo.getCartTotal().getGoodsCount();

        return BaseRespVo.ok(cartGoodsCount);

    }


    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBo bo, HttpSession session) {
        // 查询当前用户所有订单信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);
        // 获取用户id，批量修改的商品id

        WxCartTotalListVo vo = cartService.checked(bo, uid);

        return BaseRespVo.ok(vo);

    }


    @PostMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateBo bo, HttpSession session) {

        // 更新购物车的信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);
        // 更新购物车的信息

        Integer status = cartService.update(bo, uid);

        WxCartTotalListVo vo = cartService.list(uid);

        if (status == 0) return BaseRespVo.ok(vo);

        return BaseRespVo.fail("更新购物车信息失败");

    }


    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CartDeleteBo bo, HttpSession session) {
        // 更新购物车的信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        List productIds = bo.getProductIds();

        Integer status = cartService.delete(productIds, uid);

        WxCartTotalListVo vo = cartService.list(uid);

        if (status == 0) return BaseRespVo.ok(vo);

        return BaseRespVo.fail("购物车删除失败");
    }


    @PostMapping("add")
    public BaseRespVo add(@RequestBody CartAddBo bo, HttpSession session) {
        // 添加购物车的信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        Integer count = cartService.add(bo, uid);

        return BaseRespVo.ok(count);

    }


    /**
     * 立即购买
     *
     * @param bo
     * @param session
     * @return
     */
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody CartAddBo bo, HttpSession session) {

        // 立即购买之后无视其他，直接把当前的东西添加到购物车，返回购物车id

        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        Integer cartId = cartService.fastadd(bo, uid);

        return BaseRespVo.manual(0, "成功", cartId);

    }

    /**
     * 下单
     */
    @RequestMapping("checkout")
    public BaseRespVo checkout(HttpSession session, CartCheckoutIdBo bo) {
        // 添加购物车的信息
        Integer uid = (Integer) session.getAttribute("uid");

        if (uid == null) return BaseRespVo.manual(501, "请登录", null);

        WxCheckoutTotalVo vo = cartService.checkout(bo, uid);

        return BaseRespVo.ok(vo);

    }

}
